package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;
    @Autowired
    private RabbitTemplate template;

    //分页查询
    @GetMapping("/list")
    public PageDTO<Item> list(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        Page<Item> itemPage = new Page<>(page, size);

        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
        itemPage = itemService.selectPage(itemPage, wrapper);


        PageDTO<Item> pageDTO = new PageDTO<>();
        List<Item> records = itemPage.getRecords();
        long total = itemPage.getTotal();

        pageDTO.setTotal(total);
        pageDTO.setList(records);
        return pageDTO;
    }

    /**
     * 新增商品
     * @param item
     */
    @PostMapping()
    public void save(@RequestBody Item item) {
            item.setCreateTime(new Date());
            itemService.save(item);
    }

    /**
     * 根据id修改商品状态
     * @param id 商品id
     * @param status 商品状态
     */
    @PutMapping("/status/{id}/{status}")
    public void updateStatusById(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        //交换机名称
        String exchangeName = "hmall.topic";

        try {
            Item item = new Item();
            item.setId(id);
            item.setStatus(status);
            item.setUpdateTime(new Date());
            itemService.updateById(item);

            //判断当前是上架还是下架
            if (status == 1) {
                template.convertAndSend(exchangeName, "hmall.save", id);
            }
            if (status == 2) {
                template.convertAndSend(exchangeName, "hmall.delete", id);
            }
        } catch (AmqpException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 根据id查询
     * @param id 商品id
     * @return
     */
    @GetMapping("/{id}")
    public Item getById(@PathVariable("id") Long id) {
        return itemService.getById(id);
    }

    /**
     * 根据id修改
     * @param item
     */
    @PutMapping()
    public void updateById(@RequestBody Item item) {
        //默认只修改商品内容，不修改状态
        item.setStatus(null);
        item.setUpdateTime(new Date());
        itemService.updateById(item);
    }

    /**
     * 根据id删除
     * @param id  商品id
     */
    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable("id") Long id) {
            itemService.removeById(id);

    }

    /**
     * 根据id扣库存
     * @param itemId 商品id
     * @param num 数量
     */
    @PutMapping("/stock/{itemId}/{num}")
    public void stock(@PathVariable Long itemId, @PathVariable Integer num) {
        itemService.stock(itemId, num);
    }

    @PutMapping("/recover")
    public void recoverStock(@RequestBody Item item) {
        itemService.updateById(item);
    }
}
